package com.micro.controller;

import com.micro.controller.Request.MessageRequest;
import com.micro.entity.Message;
import com.micro.repository.MessageRepository;
import com.micro.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    
    @Autowired
    private MessageRepository messageRepository;

    //创建消息统一接口
    @PostMapping("/set")
    public ResponseEntity<String> createTrainMessage(@RequestBody MessageRequest request) {
        System.out.println("接收到信息"+request.getUserName());
        try {
            messageService.saveTrainTicketMessage(request);
            return ResponseEntity.ok("消息创建成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("消息创建失败");
        }
    }

    // 订单通知接口
    @PostMapping("/order-notification")
    public ResponseEntity<Boolean> sendOrderNotification(
            @RequestParam("userId") Long userId,
            @RequestParam("orderType") String orderType,
            @RequestParam(value = "orderId", required = false) Long orderId,
            @RequestParam("title") String title,
            @RequestParam("content") String content
    ) {
        try {
            System.out.println("接收到订单通知: userId=" + userId + ", orderType=" + orderType + ", orderId=" + orderId);
            
            // 根据订单类型确定消息类型
            Integer messageType;
            switch (orderType) {
                case "TRAIN_TICKET":
                case "TRAIN_TICKET_BATCH":
                    messageType = 0; // 火车票
                    break;
                case "TRAIN_MEAL":
                    messageType = 1; // 火车餐
                    break;
                case "HOTEL":
                    messageType = 2; // 酒店
                    break;
                default:
                    messageType = 0; // 默认火车票
            }
            
            // 直接创建消息，不需要通过用户名查找用户
            Message message = new Message();
            message.setUserId(userId);
            message.setMessageType(messageType);
            message.setActionType(title);
            message.setContent(content);
            message.setCreateTime(java.time.LocalDateTime.now());
            
            // 直接保存消息
            messageRepository.insert(message);
            return ResponseEntity.ok(true);
        } catch (Exception e) {
            System.err.println("订单通知发送失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.ok(false);
        }
    }

    //返回火车票消息
    @GetMapping("/train")
    public ResponseEntity<List<Message>> getMessagesByUserAndType(
            @RequestParam String username,
            @RequestParam int type
    ) {
        System.out.println(username+"获取消息"+type);
        List<Message> messages = messageService.getMessagesByUserAndType(username, type);
        return ResponseEntity.ok(messages);
    }
}
